#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_getenv.h>
#include "prototypes.h"


SDL_Surface *ecran = NULL;
SDL_Surface *imgMur = NULL;
SDL_Surface *imgPanda = NULL;
SDL_Surface *imgJoueur1 = NULL;
SDL_Surface *imgJoueur2 = NULL;
SDL_Surface *imgFiole = NULL;
SDL_Surface *imgCape = NULL;
SDL_Surface *imgFond = NULL;

SDL_Event event;

void initSDL()
{


    SDL_Init(SDL_INIT_VIDEO); // Initialisation de la SDL

    putenv("SDL_VIDEO_WINDOW_POS=0,0");

    ecran = SDL_SetVideoMode(800, 400, 32, SDL_HWSURFACE | SDL_NOFRAME );
    SDL_WM_SetCaption("SDL", NULL);



    imgMur = IMG_Load("feuille.png");
    imgPanda = IMG_Load("panda.png");
    imgJoueur1 = IMG_Load("player1.png");
    imgJoueur2 = IMG_Load("player2.png");
    imgFiole = IMG_Load("fiole.png");
    imgCape = IMG_Load("cape.png");
    imgFond = IMG_Load("fond.png");


    SDL_PollEvent(&event);
}

void afficherCarteSDL(int tab[HAUTEUR_CARTE][LARGEUR_CARTE], Personnage* perso1, Personnage* perso2, Personnage* panda)
{
    int i = 0, j = 0, c = ' ';
    SDL_Rect rect;

    SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));

	for(i = 0 ; i < HAUTEUR_CARTE ; i++)
	{

		for(j = 0 ; j < LARGEUR_CARTE ; j++)
		{

			c = tab[i][j];
			if(tab[i][j] == 'M') c = 219;

			if(perso1->invisible == 0 && perso1->x == j && perso1->y == i) c = perso1->caractere;
			if(perso2->invisible == 0 && perso2->x == j && perso2->y == i) c = perso2->caractere;
			if(panda->invisible == 0 && panda->x == j && panda->y == i) c = panda->caractere;

			//printf("%c", c);

			rect.x = j*PX_BLOC;
            rect.y = i*PX_BLOC;

            SDL_BlitSurface(imgFond, NULL, ecran, &rect);

			if(c == 219) SDL_BlitSurface(imgMur, NULL, ecran, &rect);
            else if(c == perso1->caractere) SDL_BlitSurface(imgJoueur1, NULL, ecran, &rect);
            else if(c == perso2->caractere) SDL_BlitSurface(imgJoueur2, NULL, ecran, &rect);
            else if(c == panda->caractere) SDL_BlitSurface(imgPanda, NULL, ecran, &rect);
            else if(c == 'F') SDL_BlitSurface(imgFiole, NULL, ecran, &rect);
            else if(c == 'C') SDL_BlitSurface(imgCape, NULL, ecran, &rect);


		}

		//printf("\n");
	}


    SDL_Flip(ecran);
    SDL_PollEvent(&event);
}

void closeSDL()
{
    SDL_Quit();
}


